package Java.Stream;


import java.io.*;

//对象串行化
class Student implements Serializable {
    private String name;
    private String position;
    private int age;

    public Student(String name, String position, int age) {
        this.name = name;
        this.position = position;
        this.age = age;
    }

    public String toString() {
        return name + "  " + age + "  " + position;
    }
}


//流
public class Main {
    public static void main(String[] args) {
        System.out.println("hellow world!");
//
//        byte[] bytes=new byte[1024];
//        try {
//            int len=System.in.read(bytes);
//            String s=new String(bytes,0,len);
//            //汉字=3个字节   回车=1个字节
//            System.out.println("读到了"+len+"字节");
//            System.out.println(s);
//            //一个字符
//            System.out.println("s的长度是："+s.length());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        //过滤器流
//        byte[] bytes=new byte[10];
//        for(int i=0;i<bytes.length;i++){
//            bytes[i]=(byte) i;
//        }
//
//        //DataOutputStream读写，BufferedOutputStream缓冲池
//        try {
//            DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("a.dat")));
//            int i=123456;
//            out.writeInt(i);
//            out.close();
//            DataInputStream in =new DataInputStream(new BufferedInputStream(new FileInputStream("a.dat")));
//            int j=in.readInt();
//            System.out.println(j);
//
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//

        byte[] bytes = new byte[10];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) i;
        }
//        FileOutputStream out= null;
//        try {
//            out = new FileOutputStream("a.dat");
//            out.write(bytes);
//            out.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        catch (IOException e) {
//            e.printStackTrace();
//        }

        //文本流
//        PrintWriter  out= null;
//        try {
//            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
//                    new FileOutputStream("a.txt")
//            )));
//            int i=123456;
//            out.println(i);
//            out.close();
//            //文件读取
//            BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream("utf8.txt")));
//            String s;
//
//            while ((s=in.readLine())!=null){
//                System.out.println(s);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        //对象串行化
        Student student = new Student("陈秀贤", "软件工程师", 21);
        System.out.println(student);

        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("obj.dat"));
            out.writeObject(student);
            out.close();
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("obj.dat"));
            Student s2 = (Student) in.readObject();
            System.out.println(s2);
            in.close();
            System.out.println(student == s2);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
